﻿
#if defined(_MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif

#include "ImageWnd.h"
#include <QApplication>
#include <QtGui>

#include "SciDef.h"
#include "SciTypes.h"
#include "QDebug"
#include "QMenu"
#include "QFileDialog"

#include "SciVision.h"
using namespace SCIVISION;

ImageWnd::ImageWnd(QWidget *parent)
    : QWidget(parent)
{
    long long winhandle = (this->winId());
    long winhandle1 = winhandle;
    long lRet = SciCreateImageWnd(winhandle1, &ImgWndID);
    SciSetToolbarVisible(ImgWndID, SCI_BTN_ALL, true);
    SciEnableMoveStaticOverlay(ImgWndID,false);
    SciSetScrollbarVisible(ImgWndID,false);
    

	//SciSetBgColor(ImgWndID, 153, 153, 153);
    //setAttribute(Qt::WA_TranslucentBackground,false);
    //setAttribute(Qt::WA_StyledBackground);
    //setAutoFillBackground(true);
    //使用队列连接，不阻塞nativeEvent
    connect(this,&ImageWnd::sig_LeftMouseBtbDClick,this, &ImageWnd::ShowMenu,Qt::ConnectionType::QueuedConnection);
}

ImageWnd::~ImageWnd()
{
    SciDestoryImageWnd(ImgWndID);
}

//Label尺寸大小发生改变
void ImageWnd::resizeEvent(QResizeEvent *event)
{
    //int width = this->width();
    //int height = this->height();

    //long ret = SciMoveWindow(ImgWndID, 0, 0, width, height);
    //if(ret != 0)
    //{
    //    QString msg = "加载窗口失败";
    //    this->setText(msg);
    //    this->setAlignment(Qt::AlignCenter);
    //}


    if (ImgWndID != 0)
    {
        long long lWindow = ImgWndID;
        QSize sizeTemp = event->size();
        long long lResult = SciMoveWindow(lWindow, 0, 0, sizeTemp.width() - 1, sizeTemp.height() - 1);
    }

    SciRefresh(ImgWndID);
    QWidget::resizeEvent(event);
}

//获取事件
bool ImageWnd::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
   // if (eventType == "windows_generic_MSG" || eventType == "windows_dispatcher_MSG")
    {
        MSG* msg = reinterpret_cast<MSG*>(message);

        //当界面ROI尺寸发生改变时发送信号
        if (msg->message == WM_SIZE_CHANGED_SHAPE)
        {
            emit sig_Shape_changed(msg->wParam);
            this->setFocus();
        }
        //当点击ROI时发送信号
        if (msg->message == WM_HIT_SHAPE)
        {
            emit sig_Shape_Hit(msg->wParam);
            this->setFocus();
        }
        //当左键鼠标松开
        if (msg->message == WM_MOUSEPRESS_POSITION)
        {
            emit sig_Mouse_Release(msg->wParam);
            this->setFocus();
        }
		if (msg->message == WM_NOT_HIT_SHAPE)
		{
			emit sig_NoShape_Hit();
			this->setFocus();
		}
		//<! 左键双击
		if (msg->message == WM_LMOUSEDBLCLK_POSITION)
		{
            emit sig_LeftMouseBtbDClick();
		}
       
        
        
        //右键鼠标松开
        if (msg->message == WM_RMOUSEPRESS_POSITION)
        {
            //Ctrl+右键-》菜单栏
            if (QApplication::queryKeyboardModifiers() == Qt::ControlModifier)
            {
               
            }
        }
        if (msg->message == 33)
            this->setFocus();
    }
    return false;
    //return QLabel::nativeEvent(eventType, message, result);
}

void ImageWnd::mousePressEvent(QMouseEvent* event)
{
   
    __super::mousePressEvent(event);
}

//显示SciImage
long ImageWnd::ShowSciImage(SciImage img)
{
    long ret = SciDisplayImage(ImgWndID, img);
    SciRefresh(ImgWndID);
    return ret;
}

//绘制Roi
long ImageWnd::DrawSciROI(SciROI roi)
{
    long ret = SciDrawROI(ImgWndID,roi);
    SciRefresh(ImgWndID);
    return ret;
}

//绘制overlay
long ImageWnd::DrawSciOverlay(SciOverlay overlay)
{
    long ret = SciDrawOverlay(ImgWndID, overlay);
    SciRefresh(ImgWndID);
    return ret;
}

//绘制overlay数组
long ImageWnd::DrawSciOverlayPath(SciOverlayPath overlayPath)
{
    long ret = SciDrawOverlayPath(ImgWndID, overlayPath);
    SciRefresh(ImgWndID);
    return ret;
}

//静态图层绘制overlay
long ImageWnd::DrawSciStaticOverlay(SciOverlay overlay)
{
    long ret = SciDrawStaticOverlay(ImgWndID, overlay);
    SciRefresh(ImgWndID);
    return ret;
}

//静态图层绘制overlayPath
long ImageWnd::DrawSciStaticOverlayPath(SciOverlayPath overlayPath)
{
    long ret = SciDrawStaticOverlayPath(ImgWndID, overlayPath);
    SciRefresh(ImgWndID);
    return ret;
}

//清除所有ROI
long ImageWnd::ClearAllROI()
{
    long ret = SciClearAllROI(ImgWndID);
    SciRefresh(ImgWndID);
    return ret;
}

//清除指定overlay
long ImageWnd::ClearOverlay(long overlayID)
{
    long ret = SciClearOverlay(ImgWndID,overlayID);
    SciRefresh(ImgWndID);
    return ret;
}

//清除所有overlay
long ImageWnd::ClearAllOverlay()
{
    long ret = SciClearAllOverlay(ImgWndID);
    SciRefresh(ImgWndID);
    return ret;
}

//清除所有静态图层的 Overlay
long ImageWnd::ClearAllStaticOverlay()
{
    long ret = SciClearAllStaticOverlay(ImgWndID);
    SciRefresh(ImgWndID);
    return ret;
}

//获取图像编辑区的截图
long ImageWnd::GetSciScreenShot(QString path)
{
    //const wchar_t* wPath = reinterpret_cast<const wchar_t *>(path.utf16());
    if (path.isEmpty() || path == "")//路径为空时，存截图会崩溃
    {
        return -1;
    }
   // long ret = SciScreenshotByThread(ImgWndID, path.toLocal8Bit().constData());
    return 0;
}

//获取图像编辑区的截图
long ImageWnd::GetSciScreenShot(SciImage& img)
{
    //long ret = SciScreenshot(ImgWndID,&img);
    
    return 0;
}

long ImageWnd::GetCurrentImage(SciImage& img)
{
    return SciGetCurrentImage(ImgWndID,&img);
}

long ImageWnd::DisableMoveOverlay()
{
    SciEnableMoveOverlay(ImgWndID, false);
    SciEnableMoveROI(ImgWndID, false);
    SciEnableMoveStaticOverlay(ImgWndID, false);
    return 0;
}

void ImageWnd::LoadImageFromLocal()
{
    QString file_path = QFileDialog::getOpenFileName(nullptr, tr("导入图像"), QString(), " image (*.jpg);image (*.bmp);image (*.png);all files(*.*)");
    if (file_path != "")
    {
        SciImage img;
        img.ReadImage(file_path.toLocal8Bit().constData());
        this->ShowSciImage(img);
        emit sig_RefreshWnd();
    }
}

void ImageWnd::ExportImageToLocal()
{
    QString file_path = QFileDialog::getSaveFileName(nullptr, "save image", QString(), " image (*.bmp);;all files(*.*)");
    if (file_path != "")
    {
        SciImage img;
        long ret = SciGetCurrentImage(ImgWndID, &img);
        img.SaveImage(file_path.toLocal8Bit());
    }
}

void ImageWnd::ExportShotImage()
{
    QString file_path = QFileDialog::getSaveFileName(nullptr, "save shot image", QString(), " image (*.jpg);;all files(*.*)");
    if (file_path != "")
    {
        SciImage img;
        //long ret = SciScreenshot(ImgWndID, &img);
        img.SaveImage(file_path.toLocal8Bit());
    }
}

void ImageWnd::ShowMenu()
{
    QMenu* menu = new QMenu();
    menu->setAttribute(Qt::WA_DeleteOnClose, true);
    //<! 导入图像
    QAction* action = new QAction(tr("导入图像"), menu);
    QObject::connect(action, &QAction::triggered, this, &ImageWnd::LoadImageFromLocal);
    menu->addAction(action);

    //<!  导出图像
    action = new QAction(tr("导出图像"), menu);
    QObject::connect(action, &QAction::triggered, this, &ImageWnd::ExportImageToLocal);
    menu->addAction(action);

    //<!  导出图像窗口图
    action = new QAction(tr("导出窗口图"), menu);
    QObject::connect(action, &QAction::triggered, this, &ImageWnd::ExportShotImage);
    menu->addAction(action);

    //<!  清空图层
    action = new QAction(tr("清空Overlay"), menu);
    QObject::connect(action, &QAction::triggered, [this]() {
        this->ClearAllOverlay();
        this->ClearAllStaticOverlay();
        this->ClearAllROI();
        });
    menu->addAction(action);

    menu->exec(QCursor::pos());
    menu->deleteLater();
}


